package com.hezuti.novel.parser.qidian;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;
import org.htmlparser.visitors.NodeVisitor;

import com.hezuti.novel.domain.Author;
import com.hezuti.novel.domain.Book;
import com.hezuti.novel.domain.Category;
import com.hezuti.novel.domain.Chapter;
import com.hezuti.novel.domain.Volume;
import com.hezuti.novel.parser.NovelParser;
import com.hezuti.util.DBManager;
import com.sun.org.apache.bcel.internal.classfile.Attribute;

public class QiDianNovelParser extends NovelParser {

	@Override
	protected String getEncode() {
		return "utf-8";
	}

	@Override
	protected String getDomain() {
		return "www.qidian.com";
	}
	@Override
	protected void getBookInfo() {
		long s=System.currentTimeMillis();
		try{
		NodeFilter filter=new TagNameFilter("div");
		NodeFilter attr1=new HasAttributeFilter("class", "page_site");
		NodeFilter attr2=new HasAttributeFilter("class", "booktitle");
		NodeFilter attr=new OrFilter(attr1, attr2);
		filter=new AndFilter(filter, attr);
		NodeList nodes;//=parser.parse(filter);
		parser.visitAllNodesWith(new QidianBookInfoVisitor(book));
		
		if(true)return ;
		SimpleNodeIterator iterator = nodes.elements();
		Node node=null;
		String str;
		int count=0;
		while(iterator.hasMoreNodes()){
			count++;
			node=iterator.nextNode();
			str=node.toPlainTextString().replace("\r\n", "");
			if(null==book){
				book=new Book();
			}
			if(count==1){
				int start=str.indexOf("&gt;")+4;
				int end=str.indexOf("&gt;",start);
				String fn=str.substring(start,end).trim();
				start=str.indexOf("&gt;",end+4);
				Category category=null;//DBUtil.checkCategoryName(fn,str.substring(end+4,start).trim());
				//book.setCategory(category);
				end=str.indexOf("(");
				book.setBookName(str.substring(start+4,end).trim());
				book.setBookId(str.substring(end+3,str.indexOf(")")));
			}else{
				int start=str.indexOf("作者：")+3;
				int end=str.indexOf(" ",start);
				Author author=new Author();
				author.setName((str.substring(start,end)));
				//book.setAuthor(author);
				start=str.indexOf("更新时间：")+5;
				end=str.indexOf("&nbsp;",start);
				book.setLastUpdateTime(SimpleDateFormat.getDateInstance().parse(str.substring(start,end)).getTime());
			}
		}
		System.out.println(book.getBookName());
		//获取书的每卷信息
		 filter=new TagNameFilter("div");
		 attr1=new HasAttributeFilter("id", "content");
		 filter=new AndFilter(filter,attr1);
		 parser.reset();
		 nodes=parser.extractAllNodesThatMatch(filter);
		 System.out.println("vol ; "+nodes.size());
		 iterator=nodes.elements();
		 if(iterator.hasMoreNodes()){ 
			 node=iterator.nextNode();
			 NodeVisitor v=new NodeVisitor() {
				 @Override
				public void visitTag(Tag tag) {
				if(tag.getClass().equals(Div.class)&&"title".equals(tag.getAttribute("class"))){
					System.out.println(tag.getChildren());
				}
				}
			};
			 node.accept(v);
		 }
		System.out.println("time : "+(System.currentTimeMillis()-s));
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	protected void getChapterUrlListByVol() {
		//System.out.println(content);
		
	}

	@Override
	protected Chapter buildChapter(String content) {
		// TODO Auto-generated method stub
		return null;
	}
public static void main(String[] args) {
		ArrayList<String> bookUrlList = new ArrayList<String>();
		// int i=2279798;
		int max = 1;
		DBManager db = new DBManager();
		db.getConnection();
		ResultSet rs;
		try {
			db.getConnection();
			rs = db.executeSql("SELECT source_id from book where from_site ='www.qidian.com' ORDER BY id DESC limit 1;");
			if (rs.next()) {
				max = rs.getInt(1);
			}
			db.close_DB_Object();
			for (int i = 1; i < 100000; i++) {
				bookUrlList.add("http://www.qidian.com/BookReader/" + (max + i)
						+ ".aspx");
			}
			System.out.println(max + "-----" + (max + 100000));
			new QiDianNovelParser().excute(bookUrlList, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
}

}
